/**
 * 198. 打家劫舍
 * https://leetcode-cn.com/problems/house-robber/
 */

/**
 * @param {number[]} nums
 * @return {number}
 */
function rob(nums) {
  let pre = 0;
  let doublePre = 0;
  for (let i = 0; i < nums.length; i += 1) {
    const amount = Math.max(pre, nums[i] + doublePre);
    doublePre = pre;
    pre = amount;
  }
  return pre;
}

console.log(rob([1, 2, 3, 1]) === 4);
console.log(rob([2, 7, 9, 3, 1]) === 12);
